package com.xit.hitmouse.ui;


import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.TextureView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.annotation.NonNull;

import com.xit.hitmouse.Bean.User;
import com.xit.hitmouse.R;
import com.xit.hitmouse.controller.Controller;
import com.xit.hitmouse.dao.FileHelper;
import com.xit.hitmouse.service.UserService;

import java.io.IOException;


public class MainActivity extends Activity  implements SurfaceHolder.Callback,View.OnClickListener{

        //用于播放启动时候的视频
        private SurfaceView surfaceView;
        private SurfaceHolder surfaceHolder;
        private MediaPlayer mediaPlayer;
        //开始游戏按钮
        private Button start;
        private  Button login1;
        private  Button login2;

        private LinearLayout loginComponent,topright;
         Controller controller;

         private UserService userService;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        init();
        initlayout();
        initback();
    }

    private  void init(){
        start = (Button) findViewById(R.id.start);
        login1=findViewById(R.id.login1);
        login2=findViewById(R.id.login2);
        topright=findViewById(R.id.topright);
        loginComponent=findViewById(R.id.loginComponent);
        controller = new Controller(MainActivity.this, R.raw.first_music);

    }
    private  void initlayout(){
            if(FileHelper.readFile(this) ==""){
                loginComponent.setVisibility(View.VISIBLE);
                start.setVisibility(View.GONE);//不可见且不占据空间
                topright.setVisibility(View.GONE);
        }
    }
    private  void initback(){
        controller.play();
        start.setOnClickListener(this);
        login1.setOnClickListener(this);
        login2.setOnClickListener(this);
        surfaceView = findViewById(R.id.surfaceView);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setLooping(true);
    }

    @Override
    public void onClick(View v) {
            if(v.getId() == R.id.start){
                //                点击音效
                MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.playgame);
                mediaPlayer.start();
                Intent intent = new Intent(MainActivity.this, DiffityActivity.class);
                startActivity(intent);
                //finish();
            }else if(v.getId() == R.id.login1){
                    String username = "admin";
                    String password = "123456";

                    if(username == "admin" && password == "123456"){
                        loginComponent.setVisibility(View.GONE);
                        start.setVisibility(View.VISIBLE);
                        topright.setVisibility(View.VISIBLE);
                    }
            }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        mediaPlayer.setDisplay(holder); // 将MediaPlayer与SurfaceHolder关联
        try {
            AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.start_background);
            mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            afd.close();

            mediaPlayer.prepareAsync();

            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {

            if (mediaPlayer != null) {
                mediaPlayer.release(); // 在销毁Surface时释放MediaPlayer资源
                mediaPlayer = null;
            }

    }

    //重新播放资源
    @Override
    protected void onResume() {
        controller.continuePlay();
        super.onResume();
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setLooping(true);
            surfaceHolder.addCallback(this);
        }
    }
    @Override
    protected void onStop() {
            controller.stop();
            super.onStop();
            try {
                if (mediaPlayer != null) {
                    mediaPlayer.stop();
                    mediaPlayer.release();
                    mediaPlayer = null;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    @Override
    protected void onPause() {
        super.onPause();
        if (controller != null) {
            controller.pauseMusic(); // 暂停音乐播放
        }
    }


}
